package com.starling.extend.display
{
	import com.starling.extend.core.PacketDecoder;
	
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.events.Event;
	
	public class BMovieClip extends BSprite
	{
		private var _displayObject:Object;
		private var _infos:Array;
		
		private var _isPlay:Boolean;
		private var _loop:Boolean;
		private var _currentFrame:int;
		private var _currentFrameInfo:Array;
		
		public function BMovieClip(displayObject:Object ,infos:Array)
		{
			super();
			
			_displayObject = displayObject;
			_infos = infos;
			
			updateView();
		}
		
		
		/** 当前帧 */		
		public function get currentFrame():int {return _currentFrame;}
		
		/** 总帧数 */
		public function get totalFrames():int {return _infos.length;}
		
		/** 是否循环播放 */		
		public function get loop():Boolean {return _loop;}
		public function set loop(value:Boolean):void {_loop = value;}
		
		
		
		/** 播放动画 */		
		public function play():void 
		{
			_isPlay = true;
			Starling.current.root.addEventListener(Event.ENTER_FRAME ,frameHandler);
		}
		
		/** 停止动画 */		
		public function stop():void 
		{
			_isPlay = false;
			Starling.current.root.removeEventListener(Event.ENTER_FRAME ,frameHandler);
		}
		
		
		/**
		 * 跳帧播放 
		 * @param frame
		 * 
		 */		
		public function gotoAndPlay(frame:int):void
		{
			_currentFrame = Math.max(0 ,Math.min(int(frame) ,_infos.length-1));
			play();
		}
		
		
		
		/**
		 * 跳帧停止 
		 * @param frame
		 * 
		 */		
		public function gotoAndStop(frame:int):void
		{
			_currentFrame = Math.max(0 ,Math.min(int(frame) ,_infos.length-1));
			stop();
			updateView();
		}
		
		
		/** 跳帧处理 */
		private function frameHandler(event:Event):void
		{
			if(_isPlay==false)return;
			
			_currentFrame++;
			if(_currentFrame>=_infos.length)
			{
				_currentFrame = _loop?0:_infos.length-1;
				if(_loop==false)stop();
			}
			
			updateView();
		}
		
		
		/** 更新动画 */	
		private function updateView():void
		{
			while(this.numChildren>0)
				this.removeChildAt(0);
			
			_currentFrameInfo = _infos[_currentFrame];
			var child:DisplayObject;
			var childInfo:Object;
			var childNum:int = _currentFrameInfo.length;
			var i:int=0;
			for (i;i<childNum;i++)
			{
				childInfo = _currentFrameInfo[i];
				child = _displayObject[childInfo.textureName];
				BDisplay.setInfo(child ,childInfo);
				addChild(child);
			}
		}
		
		
		
		
		/**
		 * 回收 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.current.root.removeEventListener(Event.ENTER_FRAME ,frameHandler);
			
			_infos = null;
			_isPlay = false;
			_loop = false;
			
			while(this.numChildren>0)
				this.removeChildAt(0);
			
			if(_displayObject!=null)
			{
				for (var key:String in _displayObject)
				{
					var display:DisplayObject = _displayObject[key];
					if(display.parent!=null)display.parent.removeChild(display);
					display.dispose();
					display = null;
				}
				_displayObject = null;
			}
			
			super.dispose();
			
			BSystem.gc();
		}
	}
}